package com.ydy.permission.controller;

import com.ydy.common.BaseController;
import com.ydy.common.model.Dict;
import com.ydy.common.vo.UserRoleMenuPermissionVo;
import com.ydy.permission.service.PermissionService;
import org.apache.commons.collections4.MapUtils;
import org.apache.commons.collections4.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

@RestController
public class PermissionController extends BaseController {

    private static Logger LOGGER = LoggerFactory.getLogger(PermissionController.class);


    @Autowired
    private PermissionService permissionService;

    /**
     * 查询权限管理信息
     * @Title: queryPermissionManage
     * @author: dy.yin 2021/4/9 14:40
     * @param: []
     * @return: java.lang.Object
     * @throws
     */
    @GetMapping("/queryPermissionManage")
    public Object queryPermissionManage(){
        List<UserRoleMenuPermissionVo> userRole = permissionService.queryUserRole();
        List<UserRoleMenuPermissionVo> roleMenus = permissionService.queryRoleMenus();
        List<UserRoleMenuPermissionVo> rolePermission = permissionService.queryRolePermission();
        Map<String,List<UserRoleMenuPermissionVo>> retMap = new HashedMap<>();
        retMap.put("userRole",userRole);
        retMap.put("roleMenus",roleMenus);
        retMap.put("rolePermission",rolePermission);
        return retResultVo(retMap);
    }

    /**
     * 查询用户角色配置信息
     * @return
     */
    @GetMapping("/queryUserRole")
    public Object queryUserRole(){
        List<UserRoleMenuPermissionVo> list = permissionService.queryUserRole();
        return retResultVo(list);
    }

    /**
     * 查询角色目录配置信息
     * @return
     */
    @GetMapping("/queryRoleMenus")
    public Object queryRoleMenus(){
        List<UserRoleMenuPermissionVo> list = permissionService.queryRoleMenus();
        return retResultVo(list);
    }

    /**
     * 查询角色权限配置信息
     * @return
     */
    @GetMapping("/queryRolePermission")
    public Object queryRolePermission(){
        List<UserRoleMenuPermissionVo> list = permissionService.queryRolePermission();
        return retResultVo(list);
    }

    /**
     * 保存用户角色权限
     * @Title: saveUserRole
     * @author: dy.yin 2021/4/2 13:49
     * @param: [userRoleId]
     * @return: java.lang.Object
     * @throws
     */
    @PostMapping("/saveUserRole")
    public Object saveUserRole(@RequestBody Map<String,List<String>> userRoleId){
        List<String> add = (List<String>) MapUtils.getObject(userRoleId,"add");
        List<String> del = (List<String>)MapUtils.getObject(userRoleId,"del");
        permissionService.saveUserRole(getAddOrDelList(add),getAddOrDelList(del));
        return retResultVo();
    }

    /**
     * 保存角色菜单权限
     * @Title: saveRoleMenu
     * @author: dy.yin 2021/4/2 13:49
     * @param: [roleMenuId]
     * @return: java.lang.Object
     * @throws
     */
    @PostMapping("/saveRoleMenus")
    public Object saveRoleMenu(@RequestBody Map<String,List<String>> roleMenuId){
        List<String> add = (List<String>)MapUtils.getObject(roleMenuId,"add");
        List<String> del = (List<String>)MapUtils.getObject(roleMenuId,"del");
        permissionService.saveRoleMenu(getAddOrDelList(add),getAddOrDelList(del));
        return retResultVo();
    }

    /**
     * 保存角色访问权限
     * @Title: saveRolePermission
     * @author: dy.yin 2021/4/2 13:50
     * @param: [rolePermissionId]
     * @return: java.lang.Object
     * @throws
     */
    @PostMapping("/saveRolePermission")
    public Object saveRolePermission(@RequestBody Map<String,List<String>> rolePermissionId){
        List<String> add = (List<String>)MapUtils.getObject(rolePermissionId,"add");
        List<String> del = (List<String>)MapUtils.getObject(rolePermissionId,"del");
        permissionService.saveRolePermission(getAddOrDelList(add),getAddOrDelList(del));
        return retResultVo();
    }

    private List<Map<String,String>> getAddOrDelList(List<String> add) {
        List<Map<String,String>> list = new LinkedList<>();
        add.forEach(a ->{
            Map<String,String> map = new HashedMap<>();
            map.put("first",a.split("-")[0]);
            map.put("second",a.split("-")[1]);
            list.add(map);
        });
        return list;
    }

    /**
     * 查询字典配置信息
     * @Title: queryDictList
     * @author: dy.yin 2021/4/25 8:53
     * @param: []
     * @return: java.lang.Object
     * @throws
     */
    @GetMapping("/queryDictList")
    public Object queryDictList(){
        List<Dict> dictList = permissionService.queryDictList();
        return retResultVo(dictList);
    }

}
